package com.example.service.impl;

import com.example.mapper.ArticlesMapper;
import com.example.mapper.EmpExprMapper;
import com.example.mapper.AuthorMapper;
import com.example.pojo.*;
import com.example.service.AuthorService;
import com.example.utils.JwtUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class AuthorServiceImpl implements AuthorService {
    @Autowired
   private AuthorMapper authorMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private ArticlesMapper  articlesMapper;
    @Override
    public PageResult<Author> page(AuthorQueryParam authorQueryParam){
        PageHelper.startPage(authorQueryParam.getPage(), authorQueryParam.getPageSize());
        List<Author> empList= authorMapper.list(authorQueryParam);
        Page<Author> p=(Page<Author>)  empList;
        return new PageResult<Author>(p.getTotal(),p.getResult());
    }

    @Override
    public void save(Author emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        authorMapper.innsert(emp);
    }
 @Transactional(rollbackFor = {Exception.class})
    @Override
    public void delete(List<Integer> ids) {
     articlesMapper.deleteByAuthorIds(ids);
     authorMapper.deleteByIds(ids);


    }

    @Override
    public Author getInfo(Integer id) {
       List<Articles> articles =   articlesMapper.showByauthorId(id);
       Author author =  authorMapper.getById(id);
        author.setArticles(articles);
        return author;

    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void update(Author emp) {
        emp.setUpdateTime(LocalDateTime.now());
        authorMapper.update(emp);
//        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));
//        List<AuthorArticles> exprList = emp.getExprList();
//        if(!CollectionUtils.isEmpty(exprList)){
//            exprList.forEach(authorArticles ->
//                    authorArticles.setEmpId(emp.getId()));
//        }

    }
    @Override
    public List<Author> showByauthor() {
        return authorMapper.showAll();
    }



//    @Override
//    public Login login1(Author emp) {
//        log.info("Logging in with username: {}, password: {}", emp.getUsername(), emp.getPassword());
//        Emp p = authorMapper.selectBypassword(emp);
//
//        if (p != null) {
//            Map<String,Object> claims =new HashMap<>();
//            claims.put("id",p.getId());
//            claims.put("username",p.getUsername());
//            String jwt = JwtUtils.generateJwt(claims);
//            return new Login(p.getId(), p.getUsername(), p.getName(), jwt);
//        }
//        log.warn("No employee found with username: {} and password: {}", emp.getUsername(), emp.getPassword());
//        return null;
//    }
}
